package com.hyc.myvideo.widget;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.view.animation.DecelerateInterpolator;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import com.hyc.myvideo.R;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.Animator.AnimatorListener;
import com.nineoldandroids.animation.ValueAnimator;
import com.nineoldandroids.animation.ValueAnimator.AnimatorUpdateListener;
import com.nineoldandroids.view.ViewHelper;

import static android.R.attr.width;

public class BasePopWindow extends PopupWindow {
  private Activity mContext;
  private View mRelBg;
  private View mShowView;
  private int mShowHeight;
  private boolean isP;

  public BasePopWindow(Activity context, boolean isPading) {
    super(context);
    mContext = context;
    isP = isPading;
    // 设置SelectPicPopupWindow弹出窗体的宽
    this.setWidth(LayoutParams.MATCH_PARENT);
    // 设置SelectPicPopupWindow弹出窗体的高
    this.setHeight(LayoutParams.MATCH_PARENT);
    // 设置SelectPicPopupWindow弹出窗体可点击
    this.setFocusable(true);
    this.setOutsideTouchable(true);
    // 设置SelectPicPopupWindow弹出窗体动画效果
    this.setAnimationStyle(R.style.PopupAnimation);
    // 实例化一个ColorDrawable颜色为半透明
    ColorDrawable dw = new ColorDrawable(0x00000000);
    // 设置SelectPicPopupWindow弹出窗体的背景
    this.setBackgroundDrawable(dw);
    this.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED);
    this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
  }

  @Override
  public void setContentView(View contentView) {
    View view = View.inflate(mContext, R.layout.layout_pop_window, null);
    RelativeLayout re = (RelativeLayout) view.findViewById(R.id.pop_window_content);
    RelativeLayout.LayoutParams ll =new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    //		ll.setMargins(20,0,20,0);
    if (isP) {
      re.setPadding(20, 0, 20, 20);
    }
    re.addView(contentView, ll);
    //		re.addView(contentView,RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
    mRelBg = view.findViewById(R.id.pop_window_bg);
    view.findViewById(R.id.pop_window_bgclick).setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        if (isShowing()) {
          dismiss();
        }
      }
    });
    mShowView = contentView;
    super.setContentView(view);
  }

  /***
   * 显示在页面底部
   */
  public void showBottomPop() {
    View v = mContext.getWindow().getDecorView();
    showAtLocation(v, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); // 设置layout在PopupWindow中显示的位置
    if (mShowHeight == 0) {
      mShowView.post(new Runnable() {
        @Override
        public void run() {
          mShowHeight = mShowView.getMeasuredHeight();
          showAnimation();
        }
      });
    } else {
      showAnimation();
    }
  }

  /***
   * 显示在页面中间
   */
  public void showCenter() {
    View v = mContext.getWindow().getDecorView();
    showAtLocation(v , Gravity.LEFT | Gravity.TOP, v.getWidth()/2 - width/2, v.getHeight()/2);
    if (mShowHeight == 0) {
      mShowView.post(new Runnable(){
        @Override
        public void run() {
          mShowHeight = mShowView.getMeasuredHeight();
          showAnimation();
        }
      });
    } else {
      showAnimation();
    }
  }



  @Override
  public void dismiss() {
    dismissAnimation();
  }

  public void closeView() {
    super.dismiss();
  }

  /**
   * 设置添加屏幕的背景透明度
   */
  public void setBackgroundAlpha(float bgAlpha) {
    WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
    lp.alpha = bgAlpha; // 0.0-1.0
    mContext.getWindow().setAttributes(lp);
  }

  public void showAnimation() {
    ValueAnimator animation = ValueAnimator.ofFloat(0f, 1f);
    animation.setDuration(200);
    animation.setInterpolator(new DecelerateInterpolator());
    animation.addListener(new AnimatorListener() {
      @Override
      public void onAnimationStart(Animator arg0) {
      }

      @Override
      public void onAnimationRepeat(Animator arg0) {

      }

      @Override
      public void onAnimationEnd(Animator arg0) {
      }

      @Override
      public void onAnimationCancel(Animator arg0) {

      }
    });
    animation.addUpdateListener(new AnimatorUpdateListener() {
      @Override
      public void onAnimationUpdate(ValueAnimator animation) {
        float va = (Float) animation.getAnimatedValue();
        ViewHelper.setAlpha(mRelBg, va);
        ViewHelper.setTranslationY(mShowView, mShowHeight - mShowHeight * va);
      }
    });
    animation.start();
  }

  public void dismissAnimation() {
    ValueAnimator animation = ValueAnimator.ofFloat(1f, 0f);
    animation.setDuration(200);
    animation.setInterpolator(new DecelerateInterpolator());
    animation.addListener(new AnimatorListener() {
      @Override
      public void onAnimationStart(Animator arg0) {
      }

      @Override
      public void onAnimationRepeat(Animator arg0) {

      }

      @Override
      public void onAnimationEnd(Animator arg0) {
        closeView();
      }

      @Override
      public void onAnimationCancel(Animator arg0) {

      }
    });
    animation.addUpdateListener(new AnimatorUpdateListener() {
      @Override
      public void onAnimationUpdate(ValueAnimator animation) {
        float va = (Float) animation.getAnimatedValue();
        ViewHelper.setAlpha(mRelBg, va);
        ViewHelper.setTranslationY(mShowView, mShowHeight - mShowHeight * va);
      }
    });
    animation.start();
  }
}
